package com.javaclimb.filmbuy.controller;

import com.github.pagehelper.PageInfo;
import com.javaclimb.filmbuy.common.Result;
import com.javaclimb.filmbuy.entity.GoodsInfo;
import com.javaclimb.filmbuy.entity.UserInfo;
import com.javaclimb.filmbuy.service.GoodsInfoService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/*
* 电影详情信息的controller层
* */
@RestController
@RequestMapping(value = "/goodsInfo")
public class GoodsInfoController {
    @Resource
    private GoodsInfoService goodsInfoService;

    /*
    * 增加一个电影详情
    * */
    @PostMapping
    public Result<GoodsInfo> add(@RequestBody GoodsInfo goodsInfo){
        goodsInfoService.add(goodsInfo);
        return Result.success(goodsInfo);
    }

    /*
    * 根据主键id删除一个电影详情
    * */
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Long id){
        goodsInfoService.delete(id);
        return Result.success();
    }

    /*
    * 更新一个电影详情
    * */
    @PutMapping
    public Result<GoodsInfo> update(@RequestBody GoodsInfo goodsInfo) {
        goodsInfoService.update(goodsInfo);
        return Result.success(goodsInfo);
    }

    /*
     * 根据主键id查询一个电影详情
     * */
    @GetMapping("/{id}")
    public Result<GoodsInfo> findById(@PathVariable Long id){
        GoodsInfo goodsInfo = goodsInfoService.findById(id);
        return Result.success(goodsInfo);
    }

    /**
     * 根据电影名称模糊查询电影详情对象列表，分页查询
     * @param name 电影名称，不是必选项
     * @param pageNum 要查询第几页
     * @param pageSize 每页多少条数据
     * @return 电影详情信息列表
     */
    @GetMapping("/page/{name}")
    public Result<PageInfo<GoodsInfo>> findPage(@PathVariable String name,
                                                  @RequestParam(defaultValue = "1") Integer pageNum,
                                                  @RequestParam(defaultValue = "5") Integer pageSize){
        return Result.success(goodsInfoService.findPage(name,pageNum,pageSize));
    }

    /*根据电影类型查询最新的10条电影数据*/
    @GetMapping("/findByTypeId/{typeId}")
    public Result<List<GoodsInfo>> findByTypeId(@PathVariable Integer typeId){
        return Result.success(goodsInfoService.findByTypeId(typeId));
    }
    /**
     * 热门电影
     * @param pageNum 第几页
     * @param pageSize 每页数量
     */
    @GetMapping("/recommend")
    public Result<PageInfo<GoodsInfo>> findRecommendGoods(@RequestParam(defaultValue = "1") Integer pageNum,
                                                  @RequestParam(defaultValue = "6") Integer pageSize){
        return Result.success(goodsInfoService.findRecommendGoods(pageNum,pageSize));
    }
    /**
     * 热映电影
     * @param pageNum 第几页
     * @param pageSize 每页数量
     */
    @GetMapping("/hotSales")
    public Result<PageInfo<GoodsInfo>> hotSales(@RequestParam(defaultValue = "1") Integer pageNum,
                                                @RequestParam(defaultValue = "6") Integer pageSize){
        return Result.success(goodsInfoService.findHotSalesGoods(pageNum,pageSize));
    }

    /*
     * 根据用户id和level查询已经看过的电影
     * */
    @GetMapping("/comment/{userId}/{level}")
    public Result<List<GoodsInfo>> orderGoods(@PathVariable("userId")Long userId, @PathVariable("level")Integer level){
        return Result.success(goodsInfoService.getOrderGoods(userId, level));
    }
    /**
     * 根据关键字搜索商品
     */
    @GetMapping("/searchGoods")
    public Result<List<GoodsInfo>> searchGoods(@RequestParam String text) {
        return Result.success(goodsInfoService.searchGoods(text));
    }

}
